/*
 * @Author: liuzibin
 * @Date: 2023-07-24 00:34:42
 * @Descroption: 创建商户
 */
const axios = require("axios");

async function createMerchant(params) {
  if (params.companyId > 0) {
    return params.companyId;
  }

  const str = generateRandomString();

  const response = await axios.post(
    "https://borderclear.cn/sys/create",
    {
      mer_name: params.companyName,
      category_id: 1,
      type_id: 3,
      mer_account: str,
      mer_password: str,
      mer_phone: params.phone,
      mer_address: params.address,
      commission_rate: 0,
      sort: 0,
      status: 1,
      is_bro_room: 0,
      is_audit: 0,
      is_bro_goods: 0,
      is_best: 0,
      is_trader: 0,
    },
    {
      headers: {
        Accept: "application/json, text/plain, */*",
        Referer: "http://borderclear.cn/admin/merchant/list",
        "User-Agent":
          "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
        "Content-Type": "application/json;charset=UTF-8",
      },
    }
  );

  return response.data.data.id;
}

function generateRandomString() {
  const length = 6; // 随机字符串长度
  const characters =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  let result = "";
  for (let i = 0; i < length; i++) {
    const randomIndex = Math.floor(Math.random() * characters.length);
    result += characters.charAt(randomIndex);
  }

  const timestamp = Date.now().toString(); // 当前时间戳
  const currentTime = timestamp.substring(timestamp.length - 9); // 取后9位

  return result + currentTime;
}

module.exports = createMerchant;
